package org.activiti.designer.test;

import static org.junit.Assert.*;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;

import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricDetail;
import org.activiti.engine.history.HistoricFormProperty;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricVariableUpdate;
import org.activiti.engine.impl.persistence.entity.HistoricDetailVariableInstanceUpdateEntity;
import org.activiti.engine.impl.persistence.entity.HistoricVariableInstanceEntity;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.ActivitiRule;
import org.junit.Rule;
import org.junit.Test;


public class ProcessTestInnerFormLeaveProcess {

	private String filename = "/Users/haoxiaoliang/work/workspace-tools/bpmn20-example/src/main/resources/diagrams/innerFormLeaveProcess.bpmn";

	@Rule
	public ActivitiRule activitiRule = new ActivitiRule();

	@Test
	public void startProcess() throws Exception {
		RepositoryService repositoryService = activitiRule.getRepositoryService();
		repositoryService.createDeployment().addInputStream("innerFormLeaveProcess.bpmn20.xml",
				new FileInputStream(filename)).deploy();
		
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("innerFormLeaveProcess").singleResult();
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Map<String,String> variables = new HashMap<String, String>();
		Calendar calendar = Calendar.getInstance();
		String startDate = simpleDateFormat.format(calendar.getTime());
		calendar.add(Calendar.DAY_OF_MONTH, 2);
		String endDate = simpleDateFormat.format(calendar.getTime());
		variables.put("startDate", startDate);
		variables.put("endDate", endDate);
		variables.put("reason", "公休");
		
		IdentityService identityService = activitiRule.getIdentityService();
		identityService.setAuthenticatedUserId("wayne");
		
		FormService formService = activitiRule.getFormService();
		ProcessInstance processInstance = formService.submitStartFormData(processDefinition.getId(), variables);
		assertNotNull(processInstance);
		
		//1 业务经理审批
		TaskService taskService = activitiRule.getTaskService();
		Task deptTask = taskService.createTaskQuery().taskCandidateGroup("deptLeader").singleResult();
		variables = new HashMap<String, String>();
		variables.put("deptLeaderApprove", "true");
		formService.submitTaskFormData(deptTask.getId(), variables);
		
		//2 人事经理审批
		Task hrTask = taskService.createTaskQuery().taskCandidateGroup("hrLeader").singleResult();
		variables = new HashMap<String, String>();
		variables.put("hrApprove", "true");
		formService.submitTaskFormData(hrTask.getId(), variables);
		
		
		Task reportBackTask = taskService.createTaskQuery().taskAssignee("wayne").singleResult();
		
		variables = new HashMap<String, String>();
		variables.put("reportBackDate", simpleDateFormat.format(calendar.getTime()));
		formService.submitTaskFormData(reportBackTask.getId(), variables);
		
		HistoryService historyService = activitiRule.getHistoryService();
		HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().finished().singleResult();
		assertNotNull(historicProcessInstance);
		Map<String, Object> historyVariables = new HashMap<String, Object>();
		
		List<HistoricDetail> historicDetails = historyService.createHistoricDetailQuery().processInstanceId(processInstance.getId()).list();
		int size = 0;
		for(HistoricDetail historicDetail : historicDetails){
			if(historicDetail instanceof HistoricFormProperty){
				HistoricFormProperty historicFormProperty = (HistoricFormProperty) historicDetail;
				historyVariables.put(historicFormProperty.getPropertyId(), historicFormProperty.getPropertyValue());
				size++;
				System.out.println("form field:taskId="+historicFormProperty.getTaskId()+","+historicFormProperty.getPropertyId()+","+historicFormProperty.getPropertyValue());
			}else if(historicDetail instanceof HistoricVariableUpdate){
				HistoricDetailVariableInstanceUpdateEntity historicDetailVariableInstanceUpdateEntity = (HistoricDetailVariableInstanceUpdateEntity) historicDetail;
				//HistoricVariableUpdate variableInstanceUpdateEntity = (HistoricVariableUpdate) historicDetail;
				//historyVariables.put(variableInstanceUpdateEntity.getPropertyId(), historicFormProperty.getPropertyValue());
				System.out.println("variable:taskId="+historicDetailVariableInstanceUpdateEntity.getTaskId()+","+historicDetailVariableInstanceUpdateEntity.getName()+","+historicDetailVariableInstanceUpdateEntity.getValue());
				historyVariables.put(historicDetailVariableInstanceUpdateEntity.getName(), historicDetailVariableInstanceUpdateEntity.getValue());
			}
		}
		System.out.println(size+",DB:"+historicDetails.size());
		
		System.out.println(historyVariables);
		
	}
}